package com.gestocasa.view.dialogs.areas;

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.gestocasa.model.Descriptor;
import com.gestocasa.view.dialogs.DialogBase;

public class DescriptorDialog extends DialogBase {

	private Descriptor descriptor;

	private JLabel nameLabel;
	private JLabel valueLabel;
	private JLabel orderLabel;
	private JTextField nameEdit;
	private JTextField valueEdit;
	private JTextField orderEdit;
	
	public DescriptorDialog(JDialog parent, Descriptor description) {
		super(parent, (description==null) ? "Crear descriptor" : "Modificar descriptor", true);
		setLocationRelativeTo(parent);
		setResizable(false);
		
		this.descriptor = new Descriptor(description);
	}
	
	@Override
	protected void initComponents() {
		nameLabel = new JLabel("Nombre");
		valueLabel = new JLabel("Valor");
		orderLabel = new JLabel("Orden");
		nameEdit = new JTextField();
		valueEdit = new JTextField();
		orderEdit = new JTextField();
		
		nameEdit.setText(descriptor.getName());
		valueEdit.setText(descriptor.getValue());
		orderEdit.setText(String.valueOf(descriptor.getOrder()));
	}

	@Override
	protected JPanel makePanels() {
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.add(Box.createVerticalGlue());
		mainPanel.add(nameLabel);
		mainPanel.add(nameEdit);
		mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
		mainPanel.add(valueLabel);
		mainPanel.add(valueEdit);
		mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
		mainPanel.add(orderLabel);
		mainPanel.add(orderEdit);
		mainPanel.add(Box.createVerticalGlue());
		return mainPanel;
	}

	@Override
	protected Dimension getDefaultSize() {
		return new Dimension(280, 200);
	}

	@Override
	protected void doOK() {
		descriptor.setName(nameEdit.getText());
		descriptor.setValue(valueEdit.getText());
		try {		
			descriptor.setOrder(Integer.parseInt(orderEdit.getText()));
		}
		catch(NumberFormatException e) {
			descriptor.setOrder(0);
		}
		super.doOK();
	}
	
	@Override
	protected void onKeyEnter() {
		this.doOK();
	}
	
	public Descriptor getDescriptor() {
		return descriptor;
	}
}
